(function ($) {
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function baseSetup(){
		
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview();
		if(self.row && self.flag){
			// console.log(self.flag)
			// 先置为已读
			if(self.flag == '未读'){
				makeReadFlag(self.row)
			}
			requestData(self.row)
		}else{
			console.log('未获取到消息id和消息状态')
		}
	}
	
	function makeReadFlag(rowid){
		const user = app.getState();
		var arg = {
			rw: rowid,
			loginID: user.loginID,
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID
		}
		var methodSet = 'web.INMPlatform:SetLookNotice'
		var paramDict = {
			'method': methodSet,
			'params': JSON.stringify(arg),
			'type': 'Method'
		}
		console.log(JSON.stringify(arg))
		// 设置为已读
		$.DHCWebService(paramDict, function(result) {
			plus.nativeUI.closeWaiting();
			if (result.msg != '1') {
				mui.toast(result.msg)
			}
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
			console.log(errorStr)
		});
	}
	
	function requestData(rowid){
		const user = app.getState();
		var arg = {
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			rw: rowid,
			flag: '0'
		}
		// 获取详情
		var method = 'web.INMPlatform:GetWorkNotice'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecMethod'
		}
		// console.log(JSON.stringify(arg));
		plus.nativeUI.showWaiting('加载中', {modal: false})
		$.DHCWebService(paramDict, function(result) {
			// console.log(JSON.stringify(result))
			plus.nativeUI.closeWaiting();
			updataAnnounceDetail(result['data'])
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
			console.log(errorStr)
		});
		
	}
	function updataAnnounceDetail(data){
		$('.title')[0].innerHTML = data['NoticeTitle']||"";
		$('.info')[0].innerHTML = '<span style="color:'+data['NoticeLevelColor']+'">'+data['NoticeLevelDesc']+'</span>'+
			'<span>'+data['NoticeCreatorDesc']+'</span>'+
			'<span>'+data['NoticeDate'] + '&nbsp&nbsp'  + data['NoticeTime'] + '</span>';
		$('#contentWrapper')[0].style.top = ($('.headArea')[0].clientHeight + 10) + 'px'
		var NoticeContext = data['NoticeContext']||"";
		NoticeContext = NoticeContext.replace(/☼/g, "\"")
		NoticeContext = NoticeContext.replace(/\\/g,"")
		$('.content')[0].innerHTML = NoticeContext
		// $('.fengmian')[0].innerHTML = '<img data-preview-src="" data-preview-group="1" src="'+ src + '">';
		// data.fujianArr = [{name:'这是一个假的附件数据，因为没有fujianArr字段.pdf',size:'230k',type:'pdf'}];
		
		var html = ''
		const user = app.getState();
		var TomcatUrl = user.TomcatUrl||""
		var NoticeFileUrl = data['NoticeFileUrl'] || '';
		//NoticeFileUrl: "Fileupdate/refer/pf/WorkNotice/5/04-课程培训-2-随堂练习-3-考试成绩-通过.png,Fileupdate/refer/pf/WorkNotice/5/技操打分-A是否模式.txt"
		var fileArr = NoticeFileUrl.split(',')
		var count = 0
		for (var i = 0; i < fileArr.length;i++) {
			var oneUrl = fileArr[i]
			if (oneUrl == "") {
				continue
			}
			count += 1
			var index = oneUrl.lastIndexOf("\/");  
			var fileName=oneUrl.substring(index+1,oneUrl.length);
			index = fileName.lastIndexOf(".");
			var fileType=fileName.substring(index+1,fileName.length);
			var icon = fileTypeIcon(oneUrl);
			var iconStr = '<div class="fileImg"><img src="../dependence/teach/images/'+icon+'.svg"/></div>';
			if (icon == 'img') {
				iconStr += '<div class="fileImg"><img class="photo" src="'+
					TomcatUrl+'/'+oneUrl +'" data-preview-group="1" data-preview-src="" /></div>';
			}
			html += '<li class="mui-table-view-cell" data-url="'+oneUrl+'">'+iconStr+
				'<div class="fileName">'+fileName+'</div></li>'
		}
		$('#fileNum')[0].innerHTML = count;
		$('#fileUL')[0].innerHTML = html;
		mui('.fileArea .fileName').each(function (index,element) {
			element.addEventListener('tap',fileDomTapped)
		})
		mui('.fileArea li').each(function (index,element) {
			var dom = element.querySelector('.fileImg')
			dom.addEventListener('tap',fileDomTapped)
		})
		mui('.fileArea .photo').each(function (index,element) {
			element.addEventListener('error',function(){
				this.setAttribute('data-exist',"No")
			})
		})
		$.previewImage(); //图片放大预览
	}
	function fileDomTapped(){
		var dom = this.parentNode
		var url = dom.getAttribute('data-url');
		const user = app.getState();
		var TomcatUrl = user.TomcatUrl||""
		if (TomcatUrl == '') {
			mui.toast('TomcatUrl地址不存在请联系开发')
			return
		}
		url = TomcatUrl +'/'+url
		var fileType = fileTypeIcon(url)
		if (fileType == 'img') {
			var photo = dom.querySelector('.photo')
			if (photo.getAttribute('data-exist')!= "No") {
				mui.trigger(photo,'tap')
			}else{
				mui.toast('图片不存在')
			}
			return
		}
		var label = this.innerText
		var pageName = ''
		switch (fileType){
			case "pdf":
			case "word":
			case "ppt":
				pageName = '../FilePreview/pdfReader.html'
				break;
			case "txt":
				pageName = '../WKPlatformPages/filePreview.html'
				break;
			default:
				break;
		}
		if (pageName == '') {
			mui.toast('暂不支持此类型文件')
			return
		}
		mui.checkUrlReal(url,function(){
			var webview = plus.webview.create(pageName, pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'title':label,'fileUrl':url});
			webview.show('fade-in');
		},function(){
			mui.toast('文件不存在')
		})
		
	}
	function fileTypeIcon(url){
		var icon = ''
		url = url.toLowerCase()
		if (url.endsWith(".pdf")) {
			icon = 'pdf'
		}else if (url.endsWith(".png")||url.endsWith(".jpg")||url.endsWith(".jpeg")||url.endsWith(".gif")||url.endsWith(".bmp")) {
			icon = 'img'
		}else if (url.endsWith(".doc") || url.endsWith(".docx")) {
			icon = 'word'
		}else if (url.endsWith(".ppt") || url.endsWith(".pptx")) {
			icon = 'ppt'
		}else if (url.endsWith(".txt") || url.endsWith(".json")) {
			icon = 'txt'
		}else{
			icon = 'file'
		}
		return icon
	}
})(mui);


/*
	NoticeContext: "<p><em><strong>敬爱的医护人员：</strong></em></p><p style=☼text-indent: 2em; line-height: 1.5;☼>大家好，端午节马上来临，但作为医护天使们，我们还要整成工作，为大家献上粽子，请大家慢慢享用！的弗兰克大风大浪咖啡店立刻放到的减肥的减肥好几个。</p><p>&nbsp;</p><p style=☼text-align: right;☼><span style=☼color: #2dc26b;☼>护理部</span></p><p style=☼text-align: right;☼><strong><span style=☼background-color: #fbeeb8; color: #2dc26b;☼>2023-06-21</span></strong></p>"
	NoticeCreator: "0"
	NoticeDate: "2023-06-29"
	NoticeFileUrl: "Fileupdate/refer/pf/WorkNotice/5/04-课程培训-2-随堂练习-3-考试成绩-通过.png,Fileupdate/refer/pf/WorkNotice/5/技操打分-A是否模式.txt"
	NoticeLevel: "83||1"
	NoticeStatus: "N"
	NoticeTime: "10:28:16"
	NoticeTitle: "公告标题测试，附件测试"
	NoticeWard: "3,2,1"
	rw: "5"
*/